package com.war.service.stock;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.war.dao.safeManage.UserDao;
import com.war.dao.stock.DamagesDao;
import com.war.dao.system.WarehouseDao;
import com.war.pojo.stock.DamagesInfo;
import com.war.pojo.system.User;
import com.war.pojo.system.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

@Service
public class DamagesInfoServiceImpl implements DamagesInfoService {
    @Autowired(required = false)
    DamagesDao damagesDao;
    WarehouseDao warehouseDao;
    UserDao userDao;


    @Override
    public HashMap<String, Object> index(DamagesInfo damagesInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //1 设置分页参数
        PageHelper.startPage(damagesInfo.getPage(),damagesInfo.getRow());
        String con = damagesInfo.getCondition();
        //根据用户选择的查询条件，判断用户需要查询的
        List<DamagesInfo> list = new ArrayList<DamagesInfo>();
        //判断用户输入的查询条件是否有值
        if(con.equals("")||damagesInfo.getConValue().equals("")){
            list  = damagesDao.selectAll();
        }else if (con.equals("product_name")){
            list = damagesDao.selectByProductName(damagesInfo);
        }else if (con.equals("warehouse_name")){
            list = damagesDao.selectByWareHouseName(damagesInfo);
        }
        Iterator<DamagesInfo> damagesInfoIterator= list.iterator();
        while (damagesInfoIterator.hasNext()){
            DamagesInfo damagesIn = damagesInfoIterator.next();
            Warehouse warehouse = new Warehouse();
            warehouse.setName(damagesIn.getWarehouse_name());
            User user = new User();

        }

        //3.把查询的数据转换成分页对象
        PageInfo<DamagesInfo> page = new PageInfo<DamagesInfo>(list);

        //获取分页的当前页的集合
        map.put("list",page.getList());
        //总条数
        map.put("total",page.getTotal());
        //总页数
        map.put("totalPage",page.getPages());
        //上一页
        if(page.getPrePage()==0){
            map.put("pre",1);
        }else{
            map.put("pre",page.getPrePage());
        }

        //下一页
        //保持在最后一页
        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());
        return map;
    }

    @Override
    public HashMap<String, Object> add(DamagesInfo damagesInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = damagesDao.add(damagesInfo);
        map.put("info",num);
        return map;
    }

    @Override
    public HashMap<String, Object> deleteById(DamagesInfo damagesInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = damagesDao.deleteById(damagesInfo);
        map.put("info",num);
        return map;
    }

    @Override
    public HashMap<String, Object> updateById(DamagesInfo damagesInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = damagesDao.updateById(damagesInfo);
        map.put("info",num);
        return map;
    }

    @Override
    public HashMap<String, Object> selectAll(DamagesInfo damagesInfo) {
        HashMap<String, Object> map =  new HashMap<String, Object>();
        return map;
    }

    @Override
    public HashMap<String, Object> selectById(DamagesInfo damagesInfo) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        List<DamagesInfo> list = damagesDao.selectById(damagesInfo);
        map.put("list",list);
        return map;
    }
}
